#import "ALTimelineTextEntryElement.h"
#import "ALTimelineCanvas.h"
#import "NSObject+BlockObservation.h"


#pragma mark Constants


#pragma mark -
#pragma mark Class Globals
static NSColor *ALTimelineEntryValueNormalColor;
static NSColor *ALTimelineEntryValueSelectedColor;


#pragma mark -
@interface ALTimelineTextEntryElement (ALTimelineTextElementControllerPrivateMethods)
-(void)updateValueColorForSelectionState_:(BOOL)isSelected;
@end


#pragma mark -
@implementation ALTimelineTextEntryElement

@synthesize valueView;

+(void)initialize {
	ALTimelineEntryValueNormalColor = [NSColor colorWithCalibratedWhite:0.902 alpha:1.000];
	ALTimelineEntryValueSelectedColor = [NSColor colorWithCalibratedWhite:0.090 alpha:1.000];
}

-(void)timelineElementWasAddedToTimeline {
	[super timelineElementWasAddedToTimeline];
	[[self valueView] setEditable:NO];

	[self updateValueColorForSelectionState_:NO];
	AMBlockTask selectedTask = ^(id object, NSDictionary *change) {
		[self updateValueColorForSelectionState_:[self selected]];
	};
	[self addObserverForKeyPath:ALSelectedKey task:selectedTask];
	
	AMBlockTask valueTask = ^(id object, NSDictionary *change) {
		[[[self timeline] canvas] setNeedsLayout:YES];
	};
	[self addObserverForKeyPath:@"representedObject.primaryElement.textValue" task:valueTask];
}

-(void)timelineElementDidBeginEditingAtPoint:(NSPoint)aPoint {
	[super timelineElementDidBeginEditingAtPoint:aPoint];
	[[self view] removeFromSuperview];
	[[[self timeline] canvas] addSubview:[self view] positioned:NSWindowAbove relativeTo:nil];
	[[self valueView] setEditable:YES];
	[[[self view] window] makeFirstResponder:[self valueView]];
}

-(void)timelineElementDidEndEditing {
	[super timelineElementDidEndEditing];
	[[self valueView] setEditable:NO];
}


#pragma mark -
#pragma mark Value field delegate

-(void)controlTextDidChange:(NSNotification *)aNotification {
	NSDictionary *userInfo = [aNotification userInfo];
	NSTextView *fieldEditor = [userInfo valueForKey:@"NSFieldEditor"];
	NSLayoutManager *layoutManger = [fieldEditor layoutManager];
	NSTextContainer *container = [fieldEditor textContainer];
	NSRect usedRect = [layoutManger usedRectForTextContainer:container];
	
	NSRect valueFrame = [[self valueView] frame];
	CGFloat deltaWidth = usedRect.size.width - valueFrame.size.width;
	NSRect viewFrame = [[self view] frame];
	viewFrame.size.width += deltaWidth;
	viewFrame = NSIntegralRect(viewFrame);
	
	ALTimelineCanvas *canvas = [[self timeline] canvas];
	NSRect canvasBounds = [canvas bounds];
	CGFloat endPadding = [canvas endPadding];
	viewFrame.origin.x = MAX(viewFrame.origin.x, endPadding);
	if (NSMaxX(viewFrame) > (NSMaxX(canvasBounds) - endPadding))
		viewFrame.origin.x = NSMaxX(canvasBounds) - endPadding - viewFrame.size.width;
	
	[[self view] setFrame:viewFrame];
}

-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command {
	if (command == @selector(insertNewline:)) {
		[[self timeline] stopEditing];
		return YES;
	}
	if (command == @selector(insertTab:)) {
		[[self timeline] stopEditing];
		[[[self view] window] makeFirstResponder:[[self timeline] canvas]];
		return YES;
	}
	if (command == @selector(cancelOperation:)) {
		[self discardEditing];
		[[self valueView] hideEditModeBezel];
		[[self timeline] stopEditing];
		[[[self timeline] canvas] setNeedsLayout:YES];
		[[[self view] window] makeFirstResponder:[[self timeline] canvas]];
		return YES;
	}
	return NO;
}


#pragma mark -
#pragma mark Private

-(void)updateValueColorForSelectionState_:(BOOL)isSelected {
	NSColor *valueColor = isSelected ? ALTimelineEntryValueSelectedColor : ALTimelineEntryValueNormalColor;
	[[self valueView] setTextColor:valueColor];
}

@end
